﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team (cellcloudproject@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellSpeaker_h
#define CellSpeaker_h

#include "CellPrerequisites.h"
#include "CellSpeakerConnectorHandler.h"
#include "CellPrimitive.h"
#include "CellInet4Address.h"
#include "CellInet6Address.h"
#include "CellThread.h"

namespace cell
{

class SpeakerHeartbeat;

/** 对话者描述类。
@author
	Jiangwei Xu
*/
class _CellPrivate Speaker : public GeneralAlloc
{
public:
	/** 构造函数。
	*/
	Speaker(const String& identifier);

	/** 析构函数。
	*/
	virtual ~Speaker(void);

public:
	/**
	*/
	const String& getCelletIdentifier(void) const;

	/** 向指定地址发起请求 Cellet 服务。
	*/
	bool call(const Inet4Address& address);

	/** 中断与 Cellet 的服务。
	*/
	void hangup(void);

	/**
	*/
	bool isCalled(void) const;

	/**
	*/
	const InetAddress* getAddress(void) const;

	/**
	*/
	bool speak(Primitive& primitive);

private:
	/**
	*/
	int getRetryCount(void) const;
	/**
	*/
	void setRetryCount(int count);

	/**
	*/
	uint64 getTimestamp(void) const;
	/**
	*/
	void setTimestamp(uint64 ts);

	/**
	*/
	void recordTag(const String& tag);

	/**
	*/
	void requestCheck(Packet* packet, Session* session);

	/**
	*/
	void requestCellet(Session* session);

	/**
	*/
	void startHeartbeat(Session* session);

	/**
	*/
	void processDialogue(Packet* packet, Session* session);

	/**
	*/
	void fireDialogue(const Primitive& primitive);
	/**
	*/
	void fireContacted(void);
	/**
	*/
	void fireQuitted(void);

	/**
	*/
	void fireSessionCreated(Session* session);

	/**
	*/
	void fireSessionDestroyed(Session* session);

	/**
	*/
	void fireSessionOpened(Session* session);

	/**
	*/
	void fireSessionClosed(Session* session);

private:
	String mCelletIdentifier;
	InetAddress* mAddress;
	SpeakerConnectorHandler* mHandler;
	BlockingConnector* mConnector;

	String mNucleusTag;

	CELL_MUTEX(mHeartbeatMutex);
	SpeakerHeartbeat* mHeartbeat;

	String mRemoteTag;
	bool mCalled;

	CELL_MUTEX(mConnectMutex);

	int mCount;
	uint64 mTimestamp;

	bool mHung;

	friend class SpeakerConnectorHandler;
	friend class TalkServiceDaemon;
};



/** Speaker 心跳控制。
*/
class _CellPrivate SpeakerHeartbeat : public Thread
{
public:
	/**
	*/
	SpeakerHeartbeat(Session* session);

	/**
	*/
	virtual ~SpeakerHeartbeat(void);

public:
	/**
	*/
	void run(void);

	/**
	*/
	void stop(void);

	/**
	*/
	bool isRunning(void) const;

private:
	Session* mSession;
	bool mRunning;
	bool mSpinning;

	CELL_MUTEX(mMutex);
};

} // end namespace cell

#endif // CellSpeaker_h
